#!/usr/bin/python2

print "-------------------- command params --------------------"
import sys
print "current script : %s" % sys.argv[0]
print "total params  : %d" % len(sys.argv)
# print "the first param : %s" % sys.argv[1]
for i in sys.argv:
    print i


print "-------------------- list --------------------"
# fruit = list()
fruit = ["apple", "pear", "banana", "guava"]
# for i in range(len(fruit)):   # not concise enough
#    print i,":",fruit[i]
for i, name in enumerate(fruit):        # good !
    print i,":",name

print "-------------------- loop that some elements may be removed --------------------"
fruit = ["apple", "pear", "banana", "guava"]
i = 0
while i < len(fruit):
    if fruit[i] == 'pear':
        fruit.pop(i)           # delete it, now i point to next, not increase it
        # continue
    else:
        print("i like eating %s" % fruit[i])
        i += 1                  # point to next
        

print "-------------------- dict --------------------"
mydict = {"name":"Wybie", "age":25, "home":"china"}
# for i in mydict.keys():
#     print mydict[i]
for k,v in mydict.items():   
    print k,v


print "-------------------- set --------------------"
# Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
# s = set()     # empty set
s = set([1,2,3])
s.add(4)
s.add(3)
s.remove(1)
print s         #set([1,2,3,4])
print "4 in s : ",4 in s

s.add((1,2,3))  # s.add([1,2,3]) is error 
print s

# old_set = set([1,2,3])
# new_set = old_set.copy() 
s1 = set([1,2,3])
s2 = set([2,4])
print s1 & s2   # set([2])
print s1 | s2   # set([1,2,3,4])


print "-------------------- multidimensional array --------------------"
rows_num = 5                    # 5x10
cols_num = 10
dots = [[None]*cols_num for i in range(rows_num)]
dots[0][1] = "hello"
dots[1][1] = "world"
print dots


print "-------------------- others --------------------"
# join list 
fruit = ["apple", "pear", "banana", "guava"]
print("join fruit list : "+'-'.join(fruit))

# str.rjust(width[, fillchar]) # default fillchar is space
str = "this is an example...."
print str.rjust(30)

# zip
x = [1,2,3]
y = [4,5,6]
# for i in zip(x,y):
#     print(i)
for a,b in zip(x,y):
    print(a,b)
    
# ternary operator
print "ternary operator"
i = 7
result = "greater" if i>5 else "less"
print "i=%d, %s than %d" % (i, result, 5)



# reverse 
print "original fruit: ", fruit
fruit.reverse()
print "after reverse : ", fruit


# StringIO
import StringIO
text = StringIO.StringIO()
print >>text, "hello world"
print >>text
print >>text, "==========="        # append 
print text.getvalue()


print "print to file : out.txt"
f = open("out.txt", "w")
print >>f, "hello world"          # print to file
print >>f
f.close()
# print("%3d %0.2f" % (year, prbbincipal), file = f)    # for python3


